package com.mcms.web.control;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

import com.mcms.service.UserService;
import com.mcms.service.kf.KFJZDetailService;
import com.mcms.service.kf.KFJZListService;
import com.mcms.service.kf.KFJZUpdateService;
import com.mcms.service.ss.SSJZDetailService;
import com.mcms.service.ss.SSJZListService;
import com.mcms.service.ss.SSJZUpdateService;
import com.mcms.service.xs.XCJZDetailService;
import com.mcms.service.xs.XCJZListService;
import com.mcms.service.xs.XCJZUpdateService;
import com.mcms.web.LoginCommand;
import com.mcms.web.vo.DWZResult;

/**
 * 接诊管理
 * @author 
 */
public class ReceiveManager extends AbstractCommandController {
	private UserService userService;

	public ReceiveManager() {
		setCommandClass(LoginCommand.class);
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	protected ModelAndView handle(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		/**参数获取*/
		String actionType = request.getParameter("actionType");// 控制跳转参数
		String userName = request.getParameter("userName");
		String deptCode = request.getParameter("deptCode");
		String customid = request.getParameter("customid");
		String flag = request.getParameter("flag");
		Map sessionMap = (Map) request.getSession().getAttribute("mcmsSession");
		Map paramMap = new HashMap();
		paramMap.put("customInfo", request.getParameter("customInfo"));// 客户信息：预约就诊号或者身份证号
		paramMap.put("connect", request.getParameter("connect"));// 联系方式：电话号码 或者 qq 或者 email
		paramMap.put("appointDate", request.getParameter("appointDate"));// 开始时间：分诊时间
		paramMap.put("appointDate1", request.getParameter("appointDate1"));// 截止时间：分诊时间
		paramMap.put("flag", flag);
		
		System.out.println("at:"+actionType+";un:"+userName+";customid:"+customid);
		
		/**登陆页跳转*/
		if(actionType!=null && actionType.trim().length()>0){
			if(actionType.equals("listCustomReceive")){
				if(deptCode.equalsIgnoreCase("xs")){// 销售部的客户信息查询列表
					
					// 接诊的客户列表查询
					List jzCustList =  new XCJZListService().execute(sessionMap, paramMap);
					request.setAttribute("listResult", jzCustList);
					return new ModelAndView("custom/xsdepart/receiveCustomList"); 
					
				}else if(deptCode.equalsIgnoreCase("mz")){// 门诊部(手术室)的客户信息查询列表
					
					// 接诊的客户列表查询
					List jzCustList =  new SSJZListService().execute(sessionMap, paramMap);
					request.setAttribute("listResult", jzCustList);
					return new ModelAndView("custom/mzdepart/receiveCustomList"); 
					
				}else if(deptCode.equalsIgnoreCase("kf")){// 客服部的客户信息查询列表
					
					// 接诊的客户列表查询
					List jzCustList = new KFJZListService().execute(sessionMap, paramMap);
					request.setAttribute("listResult", jzCustList);
					return new ModelAndView("custom/kfdepart/receiveCustomList");
				}
			}else if(actionType.equals("updateReceiveCustomPage")){//接诊页面跳转
				String customId = request.getParameter("customId");//客户号
				String doctorId = request.getParameter("doctorId");//就诊号
				paramMap.put("customId", customId);
				paramMap.put("doctorId", doctorId);
				
				if(deptCode.equalsIgnoreCase("xs")){// 销售部的接诊页面
					
					/* 查询页面中需要显示的信息：当前客户id，客户基本信息map，客户问诊信息map，
					    客户就诊记录id列表，客户回访记录ID列表，图片记录信息列表 */
					Map fzDetailMap =  new XCJZDetailService().execute(sessionMap, paramMap);
					request.setAttribute("mapResult", fzDetailMap);
					return new ModelAndView("custom/xsdepart/updateReceiveCustomPage");

				}else if(deptCode.equalsIgnoreCase("mz")){// 门诊(手术）室的接诊页面
					
					Map ssDetailMap = new SSJZDetailService().execute(sessionMap, paramMap);
					request.setAttribute("mapResult", ssDetailMap);
					return new ModelAndView("custom/mzdepart/updateReceiveCustomPage"); 
				}else if(deptCode.equalsIgnoreCase("kf")){// 客服部的接诊页面
					
					Map kfDetailMap = new KFJZDetailService().execute(sessionMap, paramMap);
					request.setAttribute("mapResult", kfDetailMap);
					return new ModelAndView("custom/kfdepart/updateReceiveCustomPage"); 
				}
			}else if(actionType.equals("updateReceiveCustom")){//接诊信息提交保存
				if(deptCode.equalsIgnoreCase("xs")){// 销售部的接诊页面
					
					//paramMap参数初始化
					
					// 保存接诊信息
					DWZResult dwzResult =  new XCJZUpdateService().execute(sessionMap, paramMap);
					
					// 添加结果信息
					request.setAttribute("dwzResult", dwzResult);
					return new ModelAndView("result");
				}if(deptCode.equalsIgnoreCase("mz")){// 门诊(手术室)部的接诊页面
					
					//paramMap参数初始化
					
					// 保存接诊信息
					DWZResult dwzResult =  new SSJZUpdateService().execute(sessionMap, paramMap);
					
					// 添加结果信息
					request.setAttribute("dwzResult", dwzResult);
					return new ModelAndView("result");
				}else if(deptCode.equalsIgnoreCase("kf")){
					//paramMap参数初始化
					
					// 保存接诊信息
					DWZResult dwzResult =  new KFJZUpdateService().execute(sessionMap, paramMap);
					
					// 添加结果信息
					request.setAttribute("dwzResult", dwzResult);
					return new ModelAndView("result");
				}
			}
			
		}
		
		/**默认跳转至登陆页面*/
		return new ModelAndView("login");

	}
}